<?php

namespace App\Http\Controllers\Mobile\Personal;
use App\Models\Common\CollOption;
use App\Models\Personal\Resume;
use App\Models\Personal\SearchEngine;
use Illuminate\Support\Facades\DB;
use Style;

class WelcomeController extends CommonController{

    /**
     * 个人账户中心
     * @return mixed
     */
    public function index(){
        $user = auth()->guard('personal')->user();
        $resumeInfo = Resume::where('uid',$user->uid)->where('default','1')->first();
        if($resumeInfo){
            $resumeInfo->education = !empty($resumeInfo->education) ? CollOption::getOptionName($resumeInfo->education,'education') : '';
            $resumeInfo->work_year = !empty($resumeInfo->work_year) ? CollOption::getOptionName($resumeInfo->work_year,'work_year') : '';
            $resumeInfo->job_status = !empty($resumeInfo->job_status) ? CollOption::getOptionName($resumeInfo->job_status,'job_status') : '';
            if(!empty($resumeInfo->avatar)){
                $resumeInfo->avatar =  asset('upload/personalavatar/'.$resumeInfo->avatar);
            }else if($resumeInfo->sex == '1'){
                $resumeInfo->avatar =  asset('default/images/mobile/layout/photo01.png');
            }else if($resumeInfo->sex == '2'){
                $resumeInfo->avatar =  asset('default/images/mobile/layout/photo02.png');
            }
        }else{
            $resumeInfo = $user;
        }
        //简历份数
        $resumeCount = DB::table('resume as t1')
            ->join('personal_users as t2', 't1.uid', '=', 't2.uid')
            ->select('t1.id')
            ->where('t1.uid',$user->uid)
            ->where('t1.resume_status','!=',4)
            ->count();
        $resumeInfo->resumeCount = ($resumeCount > 0 ) ? $resumeCount : 0 ;
        //面试通知记录条数
        $interviewCount = DB::table('interview as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_job as t3', 't1.job_id', '=', 't3.id')
            ->select('t1.id')
            ->where('t1.personal_uid',$user->uid)
            ->where('t1.status','!=',4)
            ->count();
        $resumeInfo->interviewCount = ($interviewCount > 0 ) ? $interviewCount : 0 ;
        //简历投递记录条数
        $resumeSendCount = DB::table('resume_send as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_job as t3', 't1.job_id', '=', 't3.id')
            ->select('t1.id')
            ->where('t1.personal_uid',$user->uid)
            ->where('t1.resume_id', $resumeInfo->id)
            ->where('t1.status','!=',4)
            ->count();
        $resumeInfo->resumeSendCount = ($resumeSendCount > 0 ) ? $resumeSendCount : 0 ;
        //谁浏览了我记录条数
        $browseResumeCount = DB::table('browse_resume as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_users as t3', 't1.company_uid', '=', 't3.uid')
            ->select('t1.id')
            ->where('t1.personal_uid',$user->uid)
            ->count();
        $resumeInfo->browseResumeCount = ($browseResumeCount > 0 ) ? $browseResumeCount : 0 ;
        //职位收藏夹
        $favoriteCount = DB::table('favorites as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_job as t3', 't1.job_id', '=', 't3.id')
            ->select('t1.id')
            ->where('t1.uid',$user->uid)
            ->count();
        $resumeInfo->favoriteCount = ($favoriteCount > 0 ) ? $favoriteCount : 0 ;
        //浏览职位
        $browseJobCount = DB::table('browse_job as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_users as t3', 't1.company_uid', '=', 't3.uid')
            ->join('company_job as t4', 't1.job_id', '=', 't4.id')
            ->select('t1.id')
            ->where('t1.personal_uid',$user->uid)
            ->count();
        $resumeInfo->browseJobCount = ($browseJobCount > 0 ) ? $browseJobCount : 0 ;
        //浏览企业
        $browseCompanyCount = DB::table('browse_company as t1')
            ->join('company_basic as t2', 't1.com_id', '=', 't2.id')
            ->join('company_users as t3', 't1.company_uid', '=', 't3.uid')
            ->select('t1.id')
            ->where('t1.personal_uid',$user->uid)
            ->count();
        $resumeInfo->browseCompanyCount = ($browseCompanyCount > 0 ) ? $browseCompanyCount : 0 ;
        //搜索器
        $searchEngineCount = SearchEngine::where('uid',$user->uid)->count();
        $resumeInfo->searchEngineCount = ($searchEngineCount > 0 ) ? $searchEngineCount : 0 ;
        return Style::view('mobile.personal.welcome',compact('resumeInfo'));
    }
}